package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.mapper.ItemCategoryParamMapper;
import com.taotao.pojo.ItemCategoryParam;
import com.taotao.service.ItemCategoryParamService;
import com.taotao.util.TaotaoResult;
import com.taotao.util.model.EasyUIResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * describe:
 *
 * @author hong.zheng
 * @date 2018/08/27
 */
@Service
public class ItemCategoryParamServiceImpl implements ItemCategoryParamService {
    @Autowired
    private ItemCategoryParamMapper itemCategoryParamMapper;
    @Override
    public EasyUIResult getItemCategoryParamList(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<ItemCategoryParam> list = itemCategoryParamMapper.selectItemCategoryParaList();
        PageInfo<ItemCategoryParam> pageInfo = new PageInfo<ItemCategoryParam>(list);
        long total = pageInfo.getTotal();
        EasyUIResult<ItemCategoryParam> result = new EasyUIResult<ItemCategoryParam>(total, list);
        return result;
    }

    @Override
    public TaotaoResult queryItemCategoryParamById(long categoryId) {
        //根据cid查询数据库
        List<ItemCategoryParam> list = itemCategoryParamMapper.queryItemCategoryParamById(categoryId);
        //如果查询中有结果返回查询结果
        if (list != null && !list.isEmpty()) {
            return TaotaoResult.ok(list.get(0));
        }
        //查询成功但是没有查到数据
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult saveItemCategoryParam(Long categoryId,String paramData) {
        ItemCategoryParam itemCategoryParam = new ItemCategoryParam();
        itemCategoryParam.setItemCatId(categoryId);
        itemCategoryParam.setParamData(paramData);
        itemCategoryParam.setCreated(new Date());
        itemCategoryParam.setUpdated(new Date());
        //向数据库添加数据
        itemCategoryParamMapper.insert(itemCategoryParam);

        return TaotaoResult.ok();

    }
}
